using System;
using System.Data;
using System.Collections.Generic;
using System.Text;
using BA.DAL;
using Microsoft.VisualBasic;

namespace BlazeApps.Library.Forum
{
    public class ForumChecks : Base
    {
        private BA.DAL.DBUtility DAL = new BA.DAL.DBUtility();
        private Procedures Procs = new Procedures();

        /// <summary>
        /// Determines whether the forum is secure, where security roles have been applied to it, the lookup only requires the forumid or the topicid.
        /// If both are supplied the check is still performed.
        /// </summary>
        /// <param name="ForumID">The forum ID.</param>
        /// <param name="TopicID">The topic ID.</param>
        /// <returns>
        /// 	<c>true</c> if [is forum secured] [the specified forum ID]; otherwise, <c>false</c>.
        /// </returns>
        public Boolean IsForumSecured(Guid ForumID, Guid TopicID)
        {
            DAL.CreateDBObjects(ConnectionString, Provider);

            DataTable DT = new DataTable();
            Boolean Secured = false;

            if (ForumID == Guid.Empty)
            {
                DAL.AddParameter("@TopicID", TopicID);
                DT = DAL.GetDataTable(Procs.GetForumSecuredByTopicID.ToString().Trim(), System.Data.CommandType.Text, System.Data.ConnectionState.Open);
            }
            else
            {
                DAL.AddParameter("@ForumID", ForumID);
                DT = DAL.GetDataTable(Procs.GetForumSecuredByForumID.ToString().Trim(), System.Data.CommandType.Text, System.Data.ConnectionState.Open);
            }

            if (TopicID == Guid.Empty)
            {
                DAL.AddParameter("@ForumID", ForumID);
                DT = DAL.GetDataTable(Procs.GetForumSecuredByForumID.ToString().Trim(), System.Data.CommandType.Text, System.Data.ConnectionState.Open);
            }
            else
            {
                DAL.AddParameter("@TopicID", TopicID);
                DT = DAL.GetDataTable(Procs.GetForumSecuredByTopicID.ToString().Trim(), System.Data.CommandType.Text, System.Data.ConnectionState.Open);
            }

            foreach (DataRow DR in DT.Rows)
            {
                if (IsNull(DR, "secured"))
                {
                    Secured = false;
                }
                else
                {
                    Secured = Convert.ToBoolean(DR["secured"]);
                }
            }

            return Secured;
        }

        /// <summary>
        /// Determines whether the forum is locked, where the locked flag is set, the lookup only requires the forumid or the topicid.
        /// If both are supplied the check is still performed.
        /// </summary>
        /// <param name="ForumID">The forum ID.</param>
        /// <param name="TopicID">The topic ID.</param>
        /// <returns>
        /// 	<c>true</c> if [is forum locked] [the specified forum ID]; otherwise, <c>false</c>.
        /// </returns>
        public Boolean IsForumLocked(Guid ForumID, Guid TopicID)
        {
            DAL.CreateDBObjects(ConnectionString, Provider);

            DataTable DT = new DataTable();
            Boolean Locked = false;

            if(ForumID == Guid.Empty)
            {
                DAL.AddParameter("@TopicID", TopicID);
                DT = DAL.GetDataTable(Procs.GetForumLockedByTopicID.ToString().Trim(), System.Data.CommandType.Text, System.Data.ConnectionState.Open);
            }
            else
            {
                DAL.AddParameter("@ForumID", ForumID);
                DT = DAL.GetDataTable(Procs.GetForumLockedByForumID.ToString().Trim(), System.Data.CommandType.Text, System.Data.ConnectionState.Open);
            }

            if(TopicID == Guid.Empty)
            {
                DAL.AddParameter("@ForumID", ForumID);
                DT = DAL.GetDataTable(Procs.GetForumLockedByForumID.ToString().Trim(), System.Data.CommandType.Text, System.Data.ConnectionState.Open);
            }
            else
            {
                DAL.AddParameter("@TopicID", TopicID);
                DT = DAL.GetDataTable(Procs.GetForumLockedByTopicID.ToString().Trim(), System.Data.CommandType.Text, System.Data.ConnectionState.Open);
            }

            foreach(DataRow DR in DT.Rows)
            {
                if(IsNull(DR, "locked"))
                {
                    Locked = false;
                }
                else
                {
                    Locked = Convert.ToBoolean(DR["locked"]);
                }
            }

            return Locked;
        }

        /// <summary>
        /// Determines whether the forum is moderated, the lookup only requires the forumid or the topicid.
        /// If both are supplied the check is still performed.
        /// </summary>
        /// <param name="ForumID">The forum ID.</param>
        /// <param name="TopicID">The topic ID.</param>
        /// <returns>
        /// 	<c>true</c> if [is forum moderated] [the specified forum ID]; otherwise, <c>false</c>.
        /// </returns>
        public Boolean IsForumModerated(Guid ForumID, Guid TopicID)
        {
            DAL.CreateDBObjects(ConnectionString, Provider);

            DataTable DT = new DataTable();
            Boolean Moderated = false;

            if (ForumID == Guid.Empty)
            {
                DAL.AddParameter("@TopicID", TopicID);
                DT = DAL.GetDataTable(Procs.GetForumModeratedByTopicID.ToString().Trim(), System.Data.CommandType.Text, System.Data.ConnectionState.Open);
            }
            else
            {
                DAL.AddParameter("@ForumID", ForumID);
                DT = DAL.GetDataTable(Procs.GetForumModeratedByForumID.ToString().Trim(), System.Data.CommandType.Text, System.Data.ConnectionState.Open);
            }

            if (TopicID == Guid.Empty)
            {
                DAL.AddParameter("@ForumID", ForumID);
                DT = DAL.GetDataTable(Procs.GetForumModeratedByForumID.ToString().Trim(), System.Data.CommandType.Text, System.Data.ConnectionState.Open);
            }
            else
            {
                DAL.AddParameter("@TopicID", TopicID);
                DT = DAL.GetDataTable(Procs.GetForumModeratedByTopicID.ToString().Trim(), System.Data.CommandType.Text, System.Data.ConnectionState.Open);
            }

            foreach(DataRow DR in DT.Rows)
            {
                if(IsNull(DR, "Moderated"))
                {
                    Moderated = false;
                }
                else
                {
                    Moderated = Convert.ToBoolean(DR["Moderated"]);
                }
            }

            return Moderated;
        }
    }
}
